import { Controller, Get, Post, Body } from '@nestjs/common';
import { Transaction, TransactionManager, EntityManager } from 'typeorm'
import { UserService } from './user.service'
import { UserEntity } from './user.entity'
@Controller('user')
export class UserController {
  constructor(
    private userService: UserService
  ) { }

  @Post('/creatUser')
  async creatUser(
    @Body() body: any
  ): Promise<UserEntity> {
    console.log('body', body);
    return this.userService.creatUser(body)
  }
  @Get('/userList')
  async userList(): Promise<UserEntity> {
    return this.userService.userList()
  }

  @Post('/create')
  async create(
    @Body() body: any
  ) {
    const result = await this.userService.create(body)
    console.log('result', result);
    return result
  }
  @Post('/create1')
  async create1(
    @Body() body: any
  ) {
    const result = await this.userService.create1(body)
    console.log('result', result);
    return result
  }

  //装饰器方式
  @Post('/create2')
  @Transaction()
  async create2(
    @Body() body: any,
    @TransactionManager() manager: EntityManager
  ) {
    const result = await this.userService.create2(body, manager)
    console.log('result', result);
    return result
  }

  @Get('/userAll')
  async userAll() {
    return await this.userService.userAll()
  }


  @Post('/addUserRole')
  async addUserRole(
    @Body() data: any
  ) {
    return await this.userService.addUserRole(data)
  }

  @Post('/add')
  async add(
    @Body() body: any
  ) {
    const result = await this.userService.add(body)
    console.log('result', result);
    return result
  }
}
